package com.example.qihuibao.widget.dialog

import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Spinner
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.qihuibao.R
import com.example.qihuibao.base.BaseDialogFragment
import com.example.qihuibao.bean.*
import com.example.qihuibao.constant.Global
import com.example.qihuibao.dao.MyDatabase
import com.example.qihuibao.module.main.MainActivity
import com.example.qihuibao.utils.extension.toast
import com.example.qihuibao.widget.view.CommonRecyclerView
import com.example.qihuibao.widget.view.EdittextWithText
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 *@author: cww
 *@desc:企业信息操作
 *@date: 5/16/23 10:58 AM
 */
class EmployInfoUpdateDialog() : BaseDialogFragment(R.layout.dialog_employ_info_update) {
    private var employTypeSelected = ""
    private var depId1Selected = ""
    private var statusIdSelected = ""
    private val arrayType = arrayListOf<String>()
    private val arrayDepId1 = arrayListOf<String>()
    private var list:List<RspQueryEmployInfoBean.Employ>? = null
    var empTypeList :List<EmpType>? = null
    var depList :List<Department>? = null

    override fun initView() {
        lifecycleScope.launch {
            withContext(Dispatchers.IO) {
                val bean = MyDatabase.db.enterpriseInfoDao().queryInId(Global.CorpID)
                if (bean?.EmpTypeList != null) {
                    empTypeList = bean.EmpTypeList
                }
            }
            if (empTypeList != null) {

                for (item in empTypeList!!) {
                    arrayType.add(item.EmpTypeName?:"")
                }
                employTypeSelected = empTypeList!![0].EmpTypeID!!
            }
            withContext(Dispatchers.IO) {
                depList = MyDatabase.db.departmentInfoDao().queryAll()
            }
            if (!depList.isNullOrEmpty()) {
                for (item in depList!!) {
                    arrayDepId1.add(item.DepName1?:"")
                }
                depId1Selected = depList!![0].DepID1
            }
        }
        mView?.findViewById<View>(R.id.iv_close)?.setOnClickListener { dismiss() }
        (activity as MainActivity).getMainViewModel().queryEmployInfo(ReqQueryEmployInfoBean()).observe(this) {
            if (it.RetCode == "0") {
                list = it.EmpList
                mView?.findViewById<CommonRecyclerView>(R.id.rv_department)?.init(
                    R.layout.item_employ_update,
                    LinearLayoutManager(context, RecyclerView.VERTICAL, false),
                    list,
                    {parent, pos ->
                        parent.findViewById<EdittextWithText>(R.id.et_employ_id).setText("员工编号", list!![pos].EmpID?:"") {
                            list!![pos].EmpID = it
                        }
                        parent.findViewById<EdittextWithText>(R.id.et_employ_name).setText("员工姓名", list!![pos].EmpName?:"") {
                            list!![pos].EmpName = it
                        }
                        val sp1 = parent.findViewById<Spinner>(R.id.sp_employ_type)
                        sp1?.adapter = ArrayAdapter(requireActivity(), android.R.layout.simple_list_item_1, arrayType)
                        sp1?.onItemSelectedListener = object: AdapterView.OnItemSelectedListener{
                            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                                depId1Selected = depList!![position].DepID1!!
                            }

                            override fun onNothingSelected(parent: AdapterView<*>?) {}
                        }
                        val sp2 = parent.findViewById<Spinner>(R.id.sp_dep_id1)
                        sp2?.adapter = ArrayAdapter(requireActivity(), android.R.layout.simple_list_item_1, arrayDepId1)
                        sp2?.onItemSelectedListener = object: AdapterView.OnItemSelectedListener{
                            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                                depId1Selected = depList!![position].DepID1!!
                            }

                            override fun onNothingSelected(parent: AdapterView<*>?) {}
                        }
                    }
                )
            }
        }
        mView?.findViewById<View>(R.id.btn_update)?.setOnClickListener { update() }
    }

    private fun update() {
        if (list.isNullOrEmpty()) {
            dismiss()
        }else {
            for (item in list!!) {
                if (item.EmpID.isNullOrEmpty()) {
                    activity?.toast("ID不能为空")
                    return
                }
                if (item.EmpName.isNullOrEmpty()) {
                    activity?.toast("名称不能为空")
                    return
                }
                if (employTypeSelected.isEmpty()) {
                    activity?.toast("类型不能为空")
                    return
                }
                if (depId1Selected.isEmpty()) {
                    activity?.toast("部门ID不能为空")
                    return
                }
                (activity as MainActivity).getMainViewModel().updateEmployInfo(ReqUpdateEmployInfoBean(EmpID = item.EmpID, EmpName = item.EmpName, EmpType = employTypeSelected, DepID1 = depId1Selected)).observe(this) {
                    activity?.toast(if (it.RetCode == "0") "更新成功" else "更新失败")
                }
            }
        }
    }

}